package com.example.lym.syappandroid;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import net.sf.json.JSONObject;

import org.w3c.dom.Text;

import java.io.IOException;
import java.text.ParseException;

import model.Alarm;
import model.User;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import regular.IRegulation;

public class ShowalarmActivity extends AppCompatActivity {

    Alarm alarm = new Alarm();
    TextView tv_alarmdetail_time;
    TextView tv_alarmdetail_task;
    TextView tv_alarmdetail_name;
    TextView tv_alarmdetail_detail;
    Button bt_alarmdetail_read;
    int t_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_showalarm);
        ActivityCollector.addActivity(ShowalarmActivity.this);
        Init();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityCollector.addActivity(ShowalarmActivity.this);
    }

    private void Init() {
        //获取提醒id
        Intent intent = this.getIntent();
        int a_id = intent.getIntExtra("a_id",-1);
        //Toast.makeText(ShowalarmActivity.this,a_id,Toast.LENGTH_LONG).show();
        //根据a_id获取alarm
        InitAlarm(a_id);
    }

    private void InitAlarm(final int a_id) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    RequestBody requestBody = new FormBody.Builder().add("u_id", User.getU_id()+"").add("a_id",a_id+"").build();
                    Request request = new Request.Builder().url(IRegulation.URL+"alarm/getdetail").post(requestBody).build();
                    Response response = null;
                    response = client.newCall(request).execute();
                    String str = response.body().string();
                    JSONObject jsonObject = JSONObject.fromObject(str);
                    //处理异常
                    if(IRegulation.LACKACCESS.equals(jsonObject.getString("resultCode"))){
                        Log.e("ShowalarmActivity","缺少权限");
                    }else if(IRegulation.ALARM_WRONGALARM.equals(jsonObject.getString("resultCode"))){
                        Log.e("ShowalarmActivity","错误的提醒");
                    }else if(IRegulation.FAILED.equals(jsonObject.getString("resultCode"))){
                        Log.e("ShowalarmActivity","操作失败");
                    }else if(IRegulation.SUCCESS.equals(jsonObject.getString("resultCode"))){
                        //获取数据
                        JSONObject temp = jsonObject.getJSONObject("data");
                        alarm = new Alarm(temp.getInt("a_isread")==1,temp.getInt("a_id"),temp.getString("a_name"), temp.getString("a_time")
                                ,temp.getInt("u_id"),temp.getInt("t_id"),temp.getString("a_detail"),temp.getInt("a_ispush")==1);
                    }
                    //初始化列表
                    InitData();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (ParseException e){
                    e.printStackTrace();
                }

            }
        }).start();
    }

    private void InitData(){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                //初始化控件
                tv_alarmdetail_time = (TextView) findViewById(R.id.tv_alarmdetail_time);
                tv_alarmdetail_time.setText(alarm.getA_timeString());
                tv_alarmdetail_name = (TextView) findViewById(R.id.tv_alarmdetail_name);
                tv_alarmdetail_name.setText(alarm.getA_name());
                tv_alarmdetail_detail = (TextView) findViewById(R.id.tv_alarmdetail_detail);
                tv_alarmdetail_detail.setText(alarm.getA_detail());
                //根据t_id获取任务名
                t_id = alarm.getT_id();
                InitName();
                //初始化确认按钮
                bt_alarmdetail_read = (Button) findViewById(R.id.bt_alarmdetail_read);
                bt_alarmdetail_read.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(!alarm.isA_isread()){
                            read();
                        }
                        ActivityCollector.removeActivity(ShowalarmActivity.this);
                        finish();
                    }
                });
            }
        });
    }

    private void InitName() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    RequestBody requestBody = new FormBody.Builder().add("u_id", User.getU_id()+"").add("t_id",t_id+"").build();
                    Request request = new Request.Builder().url(IRegulation.URL+"task/getDetail").post(requestBody).build();
                    Response response = null;
                    response = client.newCall(request).execute();
                    String str = response.body().string();
                    JSONObject jsonObject = JSONObject.fromObject(str);
                    //处理异常
                    if(IRegulation.LACKACCESS.equals(jsonObject.getString("resultCode"))){
                        Log.e("ShowalarmActivity","缺少权限");
                    }else if(IRegulation.ALARM_WRONGALARM.equals(jsonObject.getString("resultCode"))){
                        Log.e("ShowalarmActivity","错误的提醒");
                    }else if(IRegulation.FAILED.equals(jsonObject.getString("resultCode"))){
                        Log.e("ShowalarmActivity","操作失败");
                    }else if(IRegulation.SUCCESS.equals(jsonObject.getString("resultCode"))){
                        InitNameView(jsonObject.getJSONObject("data").getString("t_name"));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void InitNameView(final String name){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tv_alarmdetail_task = (TextView) findViewById(R.id.tv_alarmdetail_task);
                tv_alarmdetail_task.setText(name);
            }
        });
    }

    private void read() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    RequestBody requestBody = new FormBody.Builder().add("u_id", User.getU_id()+"").add("a_id",alarm.getA_id()+"").build();
                    Request request = new Request.Builder().url(IRegulation.URL+"alarm/read").post(requestBody).build();
                    Response response = null;
                    response = client.newCall(request).execute();
                    String str = response.body().string();
                    JSONObject jsonObject = JSONObject.fromObject(str);
                    //处理异常
                    if(IRegulation.LACKACCESS.equals(jsonObject.getString("resultCode"))){
                        Log.e("ShowalarmActivity","缺少权限");
                    }else if(IRegulation.ALARM_WRONGALARM.equals(jsonObject.getString("resultCode"))){
                        Log.e("ShowalarmActivity","错误的提醒");
                    }else if(IRegulation.FAILED.equals(jsonObject.getString("resultCode"))){
                        Log.e("ShowalarmActivity","操作失败");
                    }else if(IRegulation.SUCCESS.equals(jsonObject.getString("resultCode"))){

                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
